package com.otod.sys.code.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.otod.base.json.JsonSuccess;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.StringUtils;
import com.otod.commons.web.BaseController;
import com.otod.sys.code.service.ICodeService;
import com.otod.sys.vo.CodeDictVo;
import com.otod.sys.vo.CodeInfoVo;

/**
 * 码表管理
 * 
 * @author jqiu
 */
@Controller
@RequestMapping("sys/code")
public class CodeController extends BaseController {

	@Autowired
	private ICodeService codeService;

	/**
	 * 类别列表查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryDict", method = RequestMethod.GET)
	@ResponseBody
	public Page queryDict(HttpServletRequest request) {
		Map param = super.getParamMapFromRequest(request);
		Page page = new Page(request);
		return codeService.queryCodeDictForList(page, param);
	}

	/**
	 * 类别删除
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delDict", method = RequestMethod.GET)
	@ResponseBody
	public String delDict(String dictId, HttpServletRequest request) {
		codeService.dictId(dictId);
		return JsonSuccess.factory("类别删除成功");
	}

	/**
	 * 下拉列表查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryCodeDict")
	@ResponseBody
	public List<CodeDictVo> queryCodeDict(@RequestBody String codeDictNo,
			HttpServletRequest request) {
		return codeService.queryCodeDict(codeDictNo);
		
	}
	
	/**
	 * 码表信息列表查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryCodeInfo")
	@ResponseBody
	public List<CodeInfoVo> queryCodeInfo(@RequestBody String dictNo,
			HttpServletRequest request) {
		
		return codeService.queryCodeInfo(dictNo);
	}
	
	/**
	 * 码表信息列表查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "queryByPmPrjRole")
	@ResponseBody
	public Page queryByPmPrjRole(HttpServletRequest request) {
//		Map<String,Object> param = new HashMap<String,Object>();
//		param.put("codeDictNo", "pmPrjRole");
		Page page = new Page(request);
		CodeInfoVo vo = new CodeInfoVo();
		/**
		 * 拼装一条数据回前台，更改了提交模式，前台代码不变
		 * */
		vo.setCodeNo("111");
		vo.setCodeName("点击选择人员");
		List<CodeInfoVo> list = new ArrayList<CodeInfoVo>();
		list.add(vo);
		page.setAaData(list);
		page.setPageSize(1);
		page.setiTotalRecords(1);
		page.setStartIndex(1);
		return page;
	}

	/**
	 * 添加数据 向表 asmg_t_code_dict插入
	 * 
	 * @param codeNo
	 * @param codeName
	 */
	@RequestMapping(value="addCodedict")
	@ResponseBody
	public String addCodedict(String codeNo,String codeName){		
		codeService.addCodedict(codeNo, codeName);
		return JsonSuccess.factory("添加成功");
	
	}
	/**
	 *通过参数编码查询相关的信息
	 * @param codeNo  参数编码
	 * @param codeName 参数名称
	 * @return  返回一一codeDictvo数据
	 */
	@RequestMapping(value="getCodeInfoById",method = RequestMethod.GET)
	@ResponseBody
	public CodeInfoVo getCodeInfoById(String uuid){	
		return codeService.getCodeInfoById(uuid);		
	}
	/***
	 * 修改表 smg_t_code_info中的数据
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="updateCodeInfo",method = RequestMethod.POST)
	@ResponseBody
	public String updateCodeInfo(CodeInfoVo vo,HttpServletRequest request){
		
		//新增
		if(StringUtils.isEmpty(vo.getUuid())){
			//主要针对新增的时候用
			//设置uuid
			vo.setUuid(codeService.getUUID());
			vo.setCodeDictNo( request.getParameter("codeInfoId"));			
			codeService.addCodeInfo(vo);
		}else{
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("uuid", vo.getUuid());
			map.put("codeDictNo", vo.getCodeDictNo());
			map.put("codeNo", vo.getCodeNo());
			map.put("codeName", vo.getCodeName());
			map.put("indexNo", vo.getIndexNo());
			codeService.updateCodeInfo(map);
		}
		return JsonSuccess.factory("保存成功");
	}
	
	
	/**
	 * 参数项删除
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delInfo", method = RequestMethod.GET)
	@ResponseBody
	public String delInfo(String infoId, HttpServletRequest request) {
		codeService.infoId(infoId);
		return JsonSuccess.factory("参数项删除成功");
	}
}
